<template>
    <div class="main-container">
        <zl-table ref="menuTable" :request-fun="this.api.system.getMenusAll" @selection-change="selectionChange">
            <div slot="operate">
                <el-button @click="saveRoleMenu">保存</el-button>
                <el-button @click="openAddDialog()">新增</el-button>
            </div>
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <zl-table-column label="名称" prop="name"/>
            <zl-table-column label="前端路径" prop="path"/>
            <zl-table-column label="后端接口路径" prop="url"/>
            <zl-table-column label="组件名称" prop="component"/>
            <zl-table-column label="图标" prop="iconCls"/>
            <el-table-column
                    fixed="right"
                    min-width="100px"
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="openAddDialog(scope.row)">新增</el-button>
                    <el-button @click="delMenu(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </zl-table>
        <el-dialog
                title="提示"
                :visible.sync="addDialog"
                width="30%">
            <div class="addDialogCon">
                <el-form :model="menuForm" :rules="menuRules" ref="menuForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="menuForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="接口路径" prop="url">
                        <el-input v-model="menuForm.url"></el-input>
                    </el-form-item>
                    <el-form-item label="前端路径" prop="path">
                        <el-input v-model="menuForm.path"></el-input>
                    </el-form-item>
                    <el-form-item label="组件名称" prop="component">
                        <el-input v-model="menuForm.component"></el-input>
                    </el-form-item>
                    <el-form-item label="图标" prop="iconCls">
                        <el-input v-model="menuForm.iconCls"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="addMenu(menuForm)">确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
import {delMenuById, updateMenuRole} from "@/api/modules/system";

export default {
    name: "SysMenu",
    data() {
        return {
            //接口api
            api: this.$store.getters.app.api,
            //是否打开新增弹窗
            addDialog: false,
            //复选框选中的数据
            selectionData:[],
            //角色id
            rid:null,
            //菜单对象参数
            menuForm: {
                component: "",
                iconCls: "",
                name: "",
                parentId: 1,
                path: "",
                result: 0,
                url: "",
                roles: [],
                requireAuth: false,
                keepAlive: false,
                enabled: false,
            },
            //新增菜单表单校验
            menuRules: {
                name: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                url: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                path: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                component: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                iconCls: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                parentId: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
            }
        }
    },
    async mounted() {
        // 接收 事件
        this.$bus.$on('toggleRowSelectionByIds', (mIds,rid)=>{
            this.toggleRowSelectionByIds(mIds,rid)
        })
    },
    methods: {
        //添加菜单
        async addMenu(menuForm) {
            await this.$refs.menuForm.validate(async (valid) => {
                if (valid) {
                    let res = await this.api.system.insertMenu(menuForm);
                    if (res.code == 200) {
                        await this.$refs.menuTable.refresh()
                        this.addDialog = false
                    }
                } else {
                    return false;
                }
            });
        },
        //打开新增栏
        openAddDialog(data) {
            this.addDialog = true;
            if(data){
                this.menuForm.parentId = data.id
            }
        },
        //删除制定菜单
        async delMenu(data) {
            await this.$confirm("你确定要删除吗？", "删除提示", {
                confirmButtonText: "确定",
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                await this.api.system.delMenuById({id: data.id})
                await this.$refs.menuTable.refresh()
            })
        },
        //选中状态
        toggleRowSelectionByIds(rows,rid) {
            this.rid = rid;
            if (rows) {
                 this.$refs.menuTable.toggleRowSelectionByIds(rows,true);
            } else {
                 this.$refs.menuTable.toggleRowSelection();
            }
        },
        /**
         * 获取选中的数据
         * @param data
         */
        selectionChange(data){
            this.selectionData = data;
        },
        //关联角色与菜单
        async saveRoleMenu(){
            if (this.selectionData && this.selectionData.length>0){
                let mIds = this.selectionData.map(e=>e.id).join(",")+",1";
                if (this.rid){
                    await this.api.system.updateMenuRole({rid:this.rid,mIds:mIds})
                    this.$message.success("保存成功！")
                }
            }
        }
    }
}
</script>

<style scoped>
.main-container {
    width: 100%;
    height: 100%;
}

.addDialogCon {
    width: 100%;
    min-height: 200px;
}

</style>